def lengthOfLastWord(s):
    res, count = 0, 0
    for c in s:
        if c != ' ':
            res += 1
            count = res
        else:
            res = 0
    return res if res else count


print(lengthOfLastWord('Hello World'))  # 5
print(lengthOfLastWord(' '))  # 5
print(lengthOfLastWord("b a "))  # 1
